#!/bin/bash

dir_list=()
fmdisk_list=()

Parted=`which  parted`
Mount=`which  mount`
Cat=`which cat`
Xfs=`which mkfs.xfs`
Rm=`which rm`

sys_disk='dev/'`df -h | awk -F '[ /]+' '/boot/ {print $3}' | tr -d '[0-9]'|head -1`
drive_list=`fdisk -l | grep -w "/dev/sd[a-z]"| grep -v "$sys_disk" |awk '{print $2}'|awk -F ':' '{print $1}'`
drive_wc=`fdisk -l | grep -w "/dev/sd[a-z]"| grep -v "$sys_disk"| wc -l `

#### Partition The Disk ##############
for i in `seq 1 $drive_wc`
	do 
		echo "Mkdir /data$i"
		mkdir -p /data$i
		dir_list=("${dir_list[@]}" "/data$i")
	done
echo -e "+++++++++++++++++++++\n"

### Formatting The Disk ####
for disks in ${drive_list}
	do
		echo "Start build : $disks"
		${Parted} -s $disks mklabel gpt
		${Parted} -s $disks mkpart  primary  0 100%
		${Parted} -s $disks print
		format_disk=$disks'1'
		echo "$format_disk start format :"
		nohup $Xfs -f $format_disk > $format_disk.out 2>&1 &
		echo -e "+++++++++++++++++++++\n"
	done

wait 

### Add Auto Startup ####
$Cat /etc/fstab >> /etc/fstab.default
sleep 1
wait
### Disk --> UUID  ####
for uuid in ${drive_list}
	do
		format_disk=$uuid'1'
		UUIDid=`lsblk -f $format_disk | grep xfs | awk '{print $3}'`
		echo -e "\n$format_disk\n$UUIDid" >> /tmp/format.tmp
		fmdisk_list=("${fmdisk_list[@]}" "$UUIDid")
	done
wait
for i in `seq 0 $(($drive_wc-1))`
	do
		echo -e "UUID=${fmdisk_list[$i]} \t${dir_list[$i]}\txfs\tdefaults,noatime,nodiratime\t 0 0 " >> /etc/fstab
	done

echo -e "\nFormat complete !\n"

$Rm -rf format.sh
